package com.test.lock.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 可重入锁(也叫做递归锁)Demo2
 *
 * @author wangjie
 * @version V1.0
 * @date 2019/12/18
 */
public class ReenterLockDemo2 {

    public static void main(String[] args) {
        Phone1 phone1 = new Phone1();
        Thread t3 = new Thread(phone1);
        Thread t4 = new Thread(phone1);
        t3.start();
        t4.start();

    }

}

class Phone1 implements Runnable {
    private Lock lock = new ReentrantLock();

    @Override
    public void run() {
        get();
    }

    private void get() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "\tget");
            set();
        } finally {
            lock.unlock();
        }
    }

    private void set() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "\tset");
        } finally {
            lock.unlock();
        }
    }
}
